const s = {
  v: `
          attribute vec4 a_position;
          attribute vec4 a_tex;
          varying vec2 v_tex;
          void main() {
              gl_Position = a_position;
              v_tex = vec2(a_tex.x, a_tex.y);
          }
      `,
  f: `
      precision lowp float;
      uniform sampler2D u_sample;
      varying vec2 v_tex;

      void main() {
          gl_FragColor = texture2D(u_sample, v_tex);
      }
      `,
}
// 得到着色器程序
const program = getProgram(gl, s)
// 获取着色器程序中的变量
const a_position = gl.getAttribLocation(program, "a_position")
const a_tex = gl.getAttribLocation(program, "a_tex")
const u_sample = gl.getUniformLocation(program, "u_sample")
//TODO 创建顶点数据
const points = new Float32Array([
  -0.5, 0.5, 0.0, 1.0, -0.5, -0.5, 0.0, 0.0, 0.5, 0.5, 1.0, 1.0, 0.5, -0.5, 1.0,
  0.0,
])
// 创建缓冲对象
const buffer = gl.createBuffer()

const BYTES = points.BYTES_PER_ELEMENT

// 绑定缓冲对象
gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
// 填充缓冲对象
gl.bufferData(gl.ARRAY_BUFFER, points, gl.STATIC_DRAW)
// 配置变量
gl.vertexAttribPointer(a_position, 2, gl.FLOAT, false, BYTES * 4, 0)
gl.vertexAttribPointer(a_tex, 2, gl.FLOAT, false, BYTES * 4, BYTES * 2)
// 启用变量
gl.enableVertexAttribArray(a_position)
gl.enableVertexAttribArray(a_tex)

// 绘制程序

// gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4)

const img = new Image()
// img.src = " https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"
img.src = "./img.png"
img.onload = () => {
  // 创建纹理对象
  const texture = gl.createTexture()
  // 翻转图片Y轴
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1)
  // 开启纹理单元
  gl.activeTexture(gl.TEXTURE0)
  // 绑定纹理对象
  gl.bindTexture(gl.TEXTURE_2D, texture)
  // 设置纹理参数
  // 处理放大逻辑
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
  // 处理缩小逻辑
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
  // 横向
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
  // 纵向
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
  // 填充 纹理对象
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, img)

  gl.uniform1i(u_sample, 0)

  gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4)
}
